const readline = require('readline');

// 更严格的表达式验证和计算
function calculate(expression) {
  // 移除所有空格并验证基础格式
  const cleaned = expression.replace(/\s+/g, '');
  if (!/^[-+]?\d*\.?\d+[\+\-\*\/][-+]?\d*\.?\d+$/.test(cleaned)) {
    throw new Error('表达式格式错误，示例: 1+2 或 -3.5*4');
  }

  // 分割操作数和运算符
  const operatorIndex = cleaned.search(/[\+\-\*\/]/);
  const operator = cleaned[operatorIndex];
  const num1 = parseFloat(cleaned.substring(0, operatorIndex));
  const num2 = parseFloat(cleaned.substring(operatorIndex + 1));

  // 检查除数零值
  if (operator === '/' && num2 === 0) {
    throw new Error('除数不能为零');
  }

  // 执行计算
  const operations = {
    '+': (a, b) => a + b,
    '-': (a, b) => a - b,
    '*': (a, b) => a * b,
    '/': (a, b) => a / b
  };
  return operations[operator](num1, num2);
}

// 更健壮的命令行交互
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('请输入四则运算表达式（例如 -5*3.2）: ', input => {
  try {
    console.log(`结果: ${calculate(input)}`);
  } catch (error) {
    console.error(`错误: ${error.message}`);
    process.exitCode = 1; // 明确设置错误退出码
  } finally {
    rl.close();
  }
});